/*
 *所有关于tb_project_reviewspecial_group类的业务代码应在此处编写
*可使用repository.调用常用方法，获取EF/Dapper等信息
*如果需要事务请使用repository.DbContextBeginTransaction
*也可使用DBServerProvider.手动获取数据库相关信息
*用户信息、权限、角色等使用UserContext.Current操作
*tb_project_reviewspecial_groupService对增、删、改查、导入、导出、审核业务代码扩展参照ServiceFunFilter
*/
using MYData.Core.BaseProvider;
using MYData.Core.Extensions.AutofacManager;
using MYData.Entity.DomainModels;
using System.Linq;
using MYData.Core.Utilities;
using System.Linq.Expressions;
using MYData.Core.Extensions;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Http;
using MYData.System.IRepositories;
using MYData.System.IServices;
using System.Net;
using System.Threading.Tasks;
using MYData.Core.EFDbContext;
using SkiaSharp;
using System.Collections.Generic;
using Mapster;
using MYData.Entity.DomainModels.ApiEntity.Input;
using Microsoft.AspNetCore.Components.Forms;
using Microsoft.Data.SqlClient.DataClassification;
using System.Reflection.Emit;
using System;
using Microsoft.AspNetCore.Mvc;
using MYData.Entity.DomainModels.ApiEntity.detail;

namespace MYData.System.Services
{
    public partial class tb_project_reviewspecial_groupService : Itb_project_reviewspecial_groupService
    {
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly Itb_project_reviewspecial_groupRepository _repository;//访问数据库
        private readonly VOLContext _db;

        [ActivatorUtilitiesConstructor]
        public tb_project_reviewspecial_groupService(
            Itb_project_reviewspecial_groupRepository dbRepository,
            IHttpContextAccessor httpContextAccessor,
            VOLContext db
            )
        : base(dbRepository)
        {
            _httpContextAccessor = httpContextAccessor;
            _repository = dbRepository;
            _db = db;
            //多租户会用到这init代码，其他情况可以不用
            //base.Init(dbRepository);
        }
        WebResponseContent webResponse = new WebResponseContent();
        /// <summary>
        /// 专家组显示
        /// </summary>
        /// <returns></returns>
        ///WJQ 2023/4/6
        public async Task<WebResponseContent> GetList()
        {
            var list = _db.Set<tb_project_reviewspecial_group>().Where(x => x.isDel == 0);
            return webResponse.OK("专家组显示成功", list);
        }
        /// <summary>
        /// 逻辑删除
        /// </summary>
        /// <returns></returns>
        /// WJQ 2023/4/6
        public async Task<WebResponseContent> DeleteList(string id)
        {
            //根据id查询要删除的数据
            var groupInfo = _db.Set<tb_project_reviewspecial_group>().Find(id);
            //修改该字段
            groupInfo.isDel = 1;
            _db.Update(groupInfo);
            //返回
            _db.SaveChanges();
            return webResponse.OK("删除成功");
        }
        /// <summary>
        /// 详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// WJQ 2023/4/6
        public async Task<WebResponseContent> DetailInfo(string id)
        {
            //根据id查询要删除的数据
            var list = _db.Set<tb_project_reviewspecial_group>().Where(x => x.panelID == id);
            //返回
            return webResponse.OK("数据显示成功", list);
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// WJQ 2023/4/7
        public  WebResponseContent UpdateInfo(groupExpertDetail_Dto dto)
        {
            //开始事务
            _db.Database.BeginTransaction();
            try
            {
                // 将输入Dto转换为领域模型
                // 把输入DTO转换成tb_project_reviewspecial_group实体
                var grouplist = dto.Adapt<tb_project_reviewspecial_group>();
                _db.Set<tb_project_reviewspecial_group>().Update(grouplist);
                if (_db.SaveChanges() < 1)
                {
                    //回滚事务
                    _db.Database.RollbackTransaction();
                    //返回提示信息
                    return webResponse.OK("修改失败");
                }
                //提交事务
                _db.Database.CommitTransaction();
                //返回提示信息
                return webResponse.OK("修改成功",true);
            }
            catch (global::System.Exception)
            {
                _db.Database.RollbackTransaction();
                return webResponse.Error("修改失败！！！");
            }

}

        /// <summary>
        /// 专家组下拉框显示
        /// </summary>
        /// <returns></returns>
        ///WJQ 2023/4/7
        public async Task<WebResponseContent>  SelectList()
        {
            var list = from x in _db.Set<tb_project_reviewspecial_group>().Where(x => x.isDel == 0)
                       select new
                       {
                           panelID = x.panelID,
                           panelName = x.panelName,
                       };

            return webResponse.OK("专家组显示成功",list);
        }

        /// <summary>
        /// 专家组名称不能重复
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public async Task<WebResponseContent> QueryName(string name)
        {
            var list = _db.Set<tb_project_reviewspecial_group>().Where(x => x.panelName == name && x.isDel==0);
            if (list.Count()>0)
            {
                //返回提示信息
                return webResponse.OK("专家组已存在", true);
            }
            else
            {
                //返回提示信息
                return webResponse.OK("", false);
            }
        }

        /// <summary>
        /// 递归
        /// </summary>
        /// <returns></returns>
        //public WebResponseContent ReviewspecialList(string panelID)
        //{
        //    var review = from p in _db.Set<tb_project_reviewspecial_group_relation>()
        //                 join r in _db.Set<tb_project_reviewspecial>() on p.panelID equals r.panelID
        //                 select new {
                         
                                           
        //                 };
        //}
    }
}
